package com.lgy;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

/**
 * Created by fengch on 2018-03-28.
 */
public class ShiroTest {
    public static void main(String[] args) {
        // 1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory = new IniSecurityManagerFactory(
                "classpath:simp-shiro-realm.ini");

        // 2、得到SecurityManager实例 并绑定给SecurityUtils
        org.apache.shiro.mgt.SecurityManager securityManager = factory
                .getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        // 3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123456");

        // 4、登录，即身份验证
        subject.login(token);

        System.out.println("断言用户已经登录 " + subject.isAuthenticated());  // 断言用户已经登录

        System.out.println("断言用户所对应的资源 " + subject.hasRole("admin") + "----" + subject.hasRole("teacher"));  // 断言用户所对应的角色
        System.out.println("断言用户所对应的资源 " + subject.isPermitted("user:add:2"));  // 断言用户所对应的资源

        // 6、退出
        subject.logout();


    }
}
